package com.gamota.mobi.network;

import java.io.File;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import com.gamota.mobi.R;



public class DeleteFileTask extends AsyncTask<String, Void, Integer>{
	
	private IDeleteFileListener listener;
	private ProgressDialog pDialog;
	private Context context;
	private boolean showProgress = false;

	public DeleteFileTask(Context context, boolean showProgress){
		this.context = context;
		this.showProgress = showProgress;
	}
	
	public void setDeleteFileListener(IDeleteFileListener l){
		this.listener = l;
	}
	
	@Override
	protected void onPostExecute(Integer result) {
		// TODO Auto-generated method stub
		if(listener != null && showProgress){
			if(result == 0){
				listener.onDeleteFileError();
			} else {
				listener.onDeleteFileSuccess();
			}
			pDialog.dismiss();
		}
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		if(showProgress){
			pDialog = ProgressDialogManager.showProgressDialog(context, R.string.deleting);
		}
	}

	@Override
	protected Integer doInBackground(String... params) {
		boolean isDeleted = false;
		// TODO Auto-generated method stub
		for(int i=0; i<params.length; i++){
			File file = new File(params[i]);
			if(!file.exists()){
				return 0;
			}
			isDeleted = file.delete();
		}
		if(isDeleted){
			return 1;
		} else {
			return 0;
		}
	}

}
